import threading
import zmq
from .func import *

class ZmqReciver(threading.Thread):
    def __init__(self,url,queue,parse_to_quotation=False):
        self.url = url;
        self.queue = queue
        self.parse_to_quotation = parse_to_quotation;
    def run(self):
        self.zmq_context = zmq.Context()
        self.zmq_sub = self.zmq_context.socket(zmq.SUB)
        self.zmq_sub.setsockopt_string(zmq.SUBSCRIBE, 'stock.quotation.string')
        self.zmq_sub.connect(self.url)
        while True:
            topic,message = zmq_sub.recv_multipart()
            if(topic == 'stock.quotation.string'):
                quotation_string = message.decode("utf-8")
                if self.parse_to_quotation:
                    quotation = self.parseFromString(quotation_string)
                    self.queue.put(quotation)
                else:
                    self.queue.put(quotation_string)
            break;
